
from PyQt4 import QtCore, QtGui

from Brain.condition import ATTRIBUTES ,OPERATORS

class Condition(QtGui.QDialog):
	def __init__(self, condition=None, callback=None):
		QtGui.QDialog.__init__(self)
		self._callback = callback # used for signaling changes
		self._condition = condition
		self._valid_literal_text = u''
		self.setupUi(self)
		for el in ATTRIBUTES:
			if el != 'float' : self.first.addItem(el)
			self.second.addItem(el)
		for el in OPERATORS:
			self.op.addItem(el)
		if condition: self.parse_condition(condition)
		self.show()
		#self.second.addItem('float')
	
	def setupUi(self, ConditionDialog):
		ConditionDialog.setObjectName("ConditionDialog")
		ConditionDialog.resize(391, 172)
		sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
		sizePolicy.setHorizontalStretch(0)
		sizePolicy.setVerticalStretch(0)
		sizePolicy.setHeightForWidth(ConditionDialog.sizePolicy().hasHeightForWidth())
		ConditionDialog.setSizePolicy(sizePolicy)
		self.buttonBox = QtGui.QDialogButtonBox(ConditionDialog)
		self.buttonBox.setGeometry(QtCore.QRect(30, 130, 341, 32))
		self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
		self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
		self.buttonBox.setObjectName("buttonBox")
		self.first = QtGui.QComboBox(ConditionDialog)
		self.first.setGeometry(QtCore.QRect(20, 10, 351, 22))
		self.first.setObjectName("first")
		self.op = QtGui.QComboBox(ConditionDialog)
		self.op.setGeometry(QtCore.QRect(300, 40, 71, 22))
		self.op.setObjectName("op")
		self.second = QtGui.QComboBox(ConditionDialog)
		self.second.setGeometry(QtCore.QRect(20, 70, 351, 22))
		self.second.setObjectName("second")
		self.literal = QtGui.QLineEdit(ConditionDialog)
		self.literal.setGeometry(QtCore.QRect(220, 100, 141, 21))
		self.literal.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
		self.literal.setObjectName("literal")
		self.literal.setEnabled(False)
		#self.literal.setValidator(QtGui.QDoubleValidator(None))

		self.retranslateUi(ConditionDialog)
		QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), ConditionDialog.accept)
		QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), ConditionDialog.reject)
		
		## connect
		QtCore.QObject.connect(self.second, QtCore.SIGNAL("currentIndexChanged(const QString&)"), ConditionDialog.item_changed)
		QtCore.QObject.connect(self.literal, QtCore.SIGNAL("textChanged(const QString&)"), ConditionDialog.literal_text_validator)
		
		QtCore.QMetaObject.connectSlotsByName(ConditionDialog)
		ConditionDialog.setTabOrder(self.first, self.op)
		ConditionDialog.setTabOrder(self.op, self.second)
		ConditionDialog.setTabOrder(self.second, self.literal)
		ConditionDialog.setTabOrder(self.literal, self.buttonBox)

	def retranslateUi(self, ConditionDialog):
		ConditionDialog.setWindowTitle(QtGui.QApplication.translate("ConditionDialog", "Edit Condition", None, QtGui.QApplication.UnicodeUTF8))

	def item_changed(self, item):
		if item == u'float':
			self.literal.setEnabled(True)
		else:
			self.literal.setEnabled(False)
	
	def literal_text_validator(self, text):
		try:
			new_text = float(text)
			self._valid_literal_text = text[:]
		except:
			self.literal.setText(self._valid_literal_text[:])
	
	def accept(self):
		if self._condition != None:
			self._condition._first = str(self.first.currentText())
			self._condition._second = str(self.second.currentText())
			self._condition._operator = str(self.op.currentText())
			self._condition._literal = float(self.literal.text())
		print '[ConditionDialog] Changes made'
		if self._callback: self._callback()
		QtGui.QDialog.accept(self)
	
	def reject(self):
		print '[ConditionDialog] Changes discarded ...'
		QtGui.QDialog.reject(self)

	def parse_condition(self, condition):
		""" edits condition, and calls event 'callback' on completion
		"""
		self._condition = condition
		self._set_combo_item(self.first, condition._first)
		self._set_combo_item(self.second, condition._second)
		print condition._literal
		self.literal.setText(str(condition._literal))
		if condition._operator == '<': self.op.setCurrentIndex(0)
		elif condition._operator == '>': self.op.setCurrentIndex(1)
		else:
			print '[ConditionDialog] operator error for %s' % (condition._operator, )
			self.op.setCurrentIndex(0)
		#self.setVisible(True)
	
	def _set_combo_item(self, combo, item):
		index = 0
		if item in ATTRIBUTES:
			index = ATTRIBUTES.index(item)
		else:
			print '[ConditionDialog] attribute error for %s' % (item, )
		combo.setCurrentIndex(index)

if __name__ == "__main__":
	import sys
	app = QtGui.QApplication(sys.argv)
	app.setQuitOnLastWindowClosed(False)
	ui = Condition()
	ui.show()
	sys.exit(app.exec_())

